package com.luhan.fmmall.controller;
import com.luhan.fmmall.entity.ShoppingCart;
import com.luhan.fmmall.service.ShoppingCartService;
import com.luhan.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/shopcart")
@Api(value = "踢狗购物车业务相关接口",tags = "购物车管理")
public class ShopcartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("添加购物车接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",dataType = "string",required = true,value = "授权令牌"),
            @ApiImplicitParam(name = "shoppingCart",dataType = "ShoppingCart",required = true,value = "购物车对象")
    })
    @PostMapping("/add")
    public ResultVO addShoppingCart(@RequestHeader String token, @RequestBody ShoppingCart shoppingCart){
        return shoppingCartService.addShoppingCart(shoppingCart);
    }

    @ApiOperation("查询购物车列表接口")
    @ApiImplicitParam(name = "userId",dataType = "int",value = "用户id",required = true)
    @GetMapping("/list")
    public ResultVO listShoppingCart(Integer userId,@RequestHeader("token") String token){
        ResultVO resultVO = shoppingCartService.selectShopCartByUserId(userId);
        return resultVO;
    }
    @ApiOperation("修改购物车商品数量接口")
    @PutMapping("/update/{cid}/{cnum}")
    public ResultVO updateCartNumByCartId(@PathVariable("cid") int cartId,
                                          @PathVariable("cnum") int cartNum,
                                          @RequestHeader("token") String token){
        ResultVO resultVO = shoppingCartService.updateCartNumByCartId(cartId, cartNum);
        return resultVO;
    }

    @ApiOperation("删除购物车接口")
    @DeleteMapping("/delete/{cid}")
    public ResultVO deleteShopCart(@PathVariable("cid") int cartId,
                                   @RequestHeader("token") String token){
        ResultVO resultVO = shoppingCartService.deleteCartShopByCartId(cartId);
        return resultVO;
    }
    @ApiOperation("根据购物车ID集合查询购物车接口")
    @ApiImplicitParam(name = "cids",dataType = "String",value = "选择的购物车id",required = true)
    @GetMapping("/listbycids")
    public ResultVO listShopCartByCids(String cids,
                                       @RequestHeader("token") String token){
        ResultVO resultVO = shoppingCartService.listShopCartByCids(cids);
        return resultVO;
    }
}
